package com.i8office.android.zhihuixinwu.module.presenter;

import android.text.TextUtils;

import com.cyan.tools.callback.SimpleCallback;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.bean.Empl;
import com.i8office.android.zhihuixinwu.module.contract.AddOpinionContract;
import com.i8office.android.zhihuixinwu.module.contract.DetailContract;
import com.i8office.android.zhihuixinwu.module.contract.StaffContract;
import com.i8office.android.zhihuixinwu.module.model.AddOpinionModel;
import com.i8office.android.zhihuixinwu.module.model.DetailModel;
import com.i8office.android.zhihuixinwu.module.model.StaffModel;
import com.socks.library.KLog;

import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2018/7/8.
 */

public class AddOpinionPresenter extends BasePresenterImpl<AddOpinionContract.View> implements AddOpinionContract.Presenter {
    private AddOpinionContract.Model model;
    private DetailContract.Model dModel;
    private StaffContract.Model sendModel;

    public AddOpinionPresenter(AddOpinionContract.View view) {
        super(view);
        model = new AddOpinionModel();
        dModel = new DetailModel();
        sendModel = new StaffModel();
    }

    @Override
    public void addOpinion(String content) {
        addDisposable(model.addOpinion(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data == null) {
                    return;
                }
                if (data.isSucceed()) {
                    mView.succeed();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }
        }, content));
    }

    @Override
    public void saveCell(Map<String, String> params) {
        addDisposable(model.saveCell(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data == null) {
                    return;
                }
                if (data.isSucceed()) {
                    mView.saveSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                    mView.requestError();
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }
        }, params));
    }

    @Override
    public void saveMoreCell(Map<String, String> params) {
        model.saveCell(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data == null) {
                    return;
                }
                if (!data.isSucceed()) {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                        KLog.e(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
            }
        }, params);
    }

    @Override
    public void saveMoreCellArrays(List<Map<String, String>> paramsList) {
        model.saveCells(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data == null) {
                    return;
                }
                if (data.isSucceed()) {
                    mView.saveSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                    mView.requestError();
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
            }
        }, paramsList);
    }

    @Override
    public void messageHandler(Map<String, String> params) {
        addDisposable(dModel.messageHandler(new SimpleCallback<Empl>(mView) {
            @Override
            public void requestSuccess(Empl empl) {
                mView.messageHandlerResult(empl);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }
        }, params));
    }

    @Override
    public void send(Map<String, String> params) {
        addDisposable(sendModel.send(new SimpleCallback<Code>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.requestError();
            }

            @Override
            public void requestSuccess(Code data) {
                super.requestSuccess(data);
                if (data.isSucceed()) {
                    mView.sendSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    } else {
                        mView.toast(data.getMessage());
                    }
                    mView.requestError();
                }

            }
        }, params));
    }

    @Override
    public void sendNotification(Map<String, String> map) {
        addDisposable(sendModel.sendNotification(new SimpleCallback<String>(mView) {

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                KLog.e("发送消息失败" + msg);
                mView.toast(msg);
                mView.requestError();
            }

            @Override
            public void requestSuccess(String data) {
                super.requestSuccess(data);
                KLog.e("发送消息成功" + data);
                mView.sendNotifiCationResult();
            }
        }, map));
    }


}
